package at.fhj.app.activity;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
//import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import at.fhj.app.R;
import at.fhj.app.model.Exam;
import at.fhj.app.model.ExamContentHandler;
import at.fhj.app.util.Configuration;

/**
 * This activity is used to display a list of all upcoming exams.
 * @author Markus Deutsch <Markus.Deutsch.ITM09@fh-joanneum.at>
 */
public class ExamsActivity extends ListActivity {
	private ArrayList<Exam> exams;

    protected void onCreate(Bundle savedInstanceState){
    	// Set the property so XML can be parsed.
    	System.setProperty("org.xml.sax.driver","org.xmlpull.v1.sax2.Driver");
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.exams);
        
        Configuration.initFormatter();

        Bundle bundle = this.getIntent().getExtras();
		String values = bundle.getString("values");
				
		try {
			XMLReader xmlReader = XMLReaderFactory.createXMLReader();
			ExamContentHandler ech = new ExamContentHandler();
			xmlReader.setContentHandler(ech);
			xmlReader.parse(new InputSource(new StringReader(values)));
			values = ech.exams.toString();
			exams = ech.exams;
		} catch(Exception e) {
			//Log.i("ExamsActivity", "Parsing error.");
			values = getString(R.string.msg_error);
			e.printStackTrace();
		}
		
		if(exams == null){
			//Log.i("ExamsActivity", "No exams available.");
		} else {
			//Log.i("ExamsActivity", "Iterating over exams list with size "+exams.size()+".");
			List<HashMap<String, Object>> content = new ArrayList<HashMap<String, Object>>();
			String[] from = new String[] {"col_1", "col_2", "status"};
			int[] to = new int[] { R.id.item1, R.id.item2, R.id.status};
			HashMap<String, Object> map = new HashMap<String, Object>();
			
			// Map single exams to list items which are then added to ListView
			for(int i=0; i<exams.size(); i++){
				map = new HashMap<String, Object>();
		    	map.put("col_1", exams.get(i).getCourse());
		    	map.put("col_2", Configuration.SIMPLE_DATETIME.format(exams.get(i).getExamDate()));
		    	
		    	/**
		    	 * Display star, depending on status of exam.
		    	 * @TODO Implement isSignedUp() method for class Exam, to manage status centrally.
		    	 */
		    	if(exams.get(i).getStatus().equals("registered") || exams.get(i).getStatus().equals("registeredByOffice")){
		    		map.put("status", R.drawable.star);
		    	} else {
		    		map.put("status", R.drawable.star_bw);
		    	}
		    	//Log.i("ExamsActivity", "Iterated over item " + i + " with course " + exams.get(i).getCourse()+".");
		    	content.add(map);
			}
			
			SimpleAdapter adapter = new SimpleAdapter(this, content, R.layout.exams_item, from, to);
	        setListAdapter(adapter);
		}
			
    }
    
    protected void onListItemClick(ListView l, View v, int position, long id){
    	Bundle bundle = new Bundle();
		bundle.putSerializable("exam", exams.get(position));
		Intent intent = new Intent(ExamsActivity.this, ExamDetailActivity.class);
		intent.putExtras(bundle);
		startActivityForResult(intent, 0);
    }
	
}
